package com.edu.boot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edu.boot.config.UserContext;
import com.edu.boot.entity.Recruitment;
import com.edu.boot.entity.Vo.RecruitmentAllInfoVo;
import com.edu.boot.entity.Vo.ShopAndRecruitmentVo;
import com.edu.boot.enums.UserTypeEnum;
import com.edu.boot.service.RecruitmentService;
import com.edu.boot.mapper.RecruitmentMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
* @author 。
* @description 针对表【recruitment】的数据库操作Service实现
* @createDate 2023-11-16 13:53:20
*/
@Service
public class RecruitmentServiceImpl extends ServiceImpl<RecruitmentMapper, Recruitment>
    implements RecruitmentService{

    @Autowired
    RecruitmentMapper recruitmentMapper;

    @Override
    public List<RecruitmentAllInfoVo> getRecruitmentAndInfoById(Integer uid, Integer bid) {
        return recruitmentMapper.getRecruitmentAndInfoById(uid,bid);
    }

    @Override
    public List<Recruitment> getRecruitmentById(Integer id) {
        return recruitmentMapper.getRecruitmentById(id);
    }

    @Override
    public List<ShopAndRecruitmentVo> getShopAndRecruitmentInfoByBid(Integer bid){
        return recruitmentMapper.getShopAndRecruitmentInfoByBid(bid);
    }

    @Override
    public RecruitmentAllInfoVo getUserAllShopRecruitmentInfoByRid(Integer rid) {
        return recruitmentMapper.getUserAllShopRecruitmentInfoByRid(rid);
    }

    @Override
    public List<RecruitmentAllInfoVo> getUserAllInfo(){
        return recruitmentMapper.getUserAllInfo();
    }

    @Override
    public List<RecruitmentAllInfoVo> getUserAllInfoSearch(String search) {
        return recruitmentMapper.getUserAllInfoSearch(search);
    }
    
    @Override
    public IPage<Recruitment> getRecruitmentList(Page<Recruitment> page, Recruitment recruitment) {
        LambdaQueryWrapper<Recruitment> queryWrapper = new LambdaQueryWrapper<Recruitment>()
                .eq(StringUtils.isNotBlank(recruitment.getRecName()), Recruitment::getRecName, recruitment.getRecName())
                .eq(Objects.nonNull(recruitment.getRecType()), Recruitment::getRecType, recruitment.getRecType())
                .eq(Recruitment::getBId, UserContext.getUserId())
                .or(UserTypeEnum.admin.getCode()
                            .equals(UserContext.getUserId()))
                .orderByDesc(Recruitment::getCreateTime);
        return this.page(page, queryWrapper);
    }
    @Override
    public IPage<RecruitmentAllInfoVo> getRecruitmentAdminList(Page<RecruitmentAllInfoVo> page, String recName, Integer recType) {
        return recruitmentMapper.getRecrutimeAdminList(page,recName, recType);
    }
    
    /**
     * 获取职位map，用于填充信息
     * @return
     */
    @Override
    public Map<Integer, String> getRecruitmentMap() {
        List<Recruitment> list = recruitmentMapper.selectList(null);
        if(CollectionUtils.isNotEmpty(list)){
            return list.stream().collect(
                    (HashMap::new),
                    (m, v) -> m.put(v.getRecId(), v.getRecName()),
                    HashMap::putAll
            );
        }
        return Collections.emptyMap();
    }
    
    /**
     * 根据职位类型查询职位
     * @param recType
     * @return
     */
    @Override
    public List<Recruitment> getByRecType(Integer recType) {
        LambdaQueryWrapper<Recruitment> queryWrapper = new LambdaQueryWrapper<Recruitment>()
                .eq(Recruitment::getRecType, recType);
        List<Recruitment> recruitments = recruitmentMapper.selectList(queryWrapper);
    
        return recruitments;
    }
}




